package name.huzhenbo.java.objects;

import java.util.Comparator;

final class PhoneNumber implements Comparable<PhoneNumber>, Cloneable {
    private final String name;
    private final int number;
    static final Comparator NAME_ORDER = new Comparator() {

        public int compare(Object o1, Object o2) {
            // Should throw ClassCastException, this is different from equals method.
            PhoneNumber phoneNumber1 = (PhoneNumber) o1;
            PhoneNumber phoneNumber2 = (PhoneNumber) o2;

            return phoneNumber1.getName().compareTo(phoneNumber2.getName());
        }
    };

    PhoneNumber(String name, int number) {
        this.name = name;
        this.number = number;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public String getName() {
        return name;
    }

    public int getNumber() {
        return number;
    }

    @Override
    public String toString() {
        return name + ":" + new Integer(number);
    }

    public int compareTo(PhoneNumber another) {
        if (number > another.getNumber()) {
            return 1;
        }
        if (number < another.getNumber()) {
            return -1;
        }
        return 0;
    }

}
